package com.moneytalks.blazepoc.models {
	import com.moneytalks.blazepoc.dto.NoteDto;
	import com.moneytalks.blazepoc.dto.UserDto;
	import com.moneytalks.blazepoc.noteForm.NoteFormModel;
	import com.moneytalks.blazepoc.services.NotebookServiceDelegate;
	import com.moneytalks.blazepoc.services.responders.RetrieveNotesResponder;
	import com.moneytalks.blazepoc.services.responders.SaveNoteResponder;
	
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.IResponder;
	import mx.rpc.remoting.RemoteObject;
	
	[Bindable]
	public class NotesViewer extends EventDispatcher {

		private var _delegate:NotebookServiceDelegate;
		
		public var currentUser:UserDto;
		
		private var _service:RemoteObject;
		public function set service(ro:RemoteObject):void { _service = ro; }
		
		private var _allNotes:ArrayCollection;
		public function get allNotes():ArrayCollection { return _allNotes; }
		public function set allNotes(a:ArrayCollection):void { _allNotes = a; }

		public var viewerSate:String = "";
		public var noteFormModel:NoteFormModel = new NoteFormModel();
		
		public function NotesViewer(delegate:NotebookServiceDelegate){
			_delegate = delegate;
			
		}
		public function gotoNotesView():void {
			viewerSate = "";
		}
		public function gotoAddForm():void {
			viewerSate = "AddItem";
			noteFormModel.note = new NoteDto();
			noteFormModel.isNewNote = true;
		}
		public function editNote(note:NoteDto):void {
			viewerSate = "AddItem";
			noteFormModel.note = note;
			noteFormModel.isNewNote = false;
		}
		public function retriveNotes():void {
			var resp:IResponder = new RetrieveNotesResponder(this);
			_delegate.retrieveNotes(currentUser.userId, resp);
		}
		public function saveNote(content:String, url:String):void {
			var note:NoteDto = new NoteDto();
			var dto :NoteDto = new NoteDto();
			dto.content = content;
			dto.url = url;
			dto.userId = currentUser.userId;
			_delegate.saveNote(dto, new SaveNoteResponder(this));
		}
	}
}